package com.ruoyi.canteen.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.ruoyi.canteen.domain.dto.CarouseDto;
import com.ruoyi.canteen.domain.dto.MealTableDto;
import com.ruoyi.canteen.domain.vo.*;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.canteen.mapper.WeeklySetmealMapper;
import com.ruoyi.canteen.domain.WeeklySetmeal;
import com.ruoyi.canteen.service.IWeeklySetmealService;

/**
 * 每周已制定套餐记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-28
 */
@Service
public class WeeklySetmealServiceImpl implements IWeeklySetmealService
{
    @Autowired
    private WeeklySetmealMapper weeklySetmealMapper;

    /**
     * 查询每周已制定套餐记录
     *
     * @param id 每周已制定套餐记录主键
     * @return 每周已制定套餐记录
     */
    @Override
    public WeeklySetmeal selectWeeklySetmealById(Long id)
    {
        return weeklySetmealMapper.selectWeeklySetmealById(id);
    }

    /**
     * 查询每周已制定套餐记录列表
     *
     * @param weeklySetmeal 每周已制定套餐记录
     * @return 每周已制定套餐记录
     */
    @Override
    public List<WeeklySetmeal> selectWeeklySetmealList(WeeklySetmeal weeklySetmeal)
    {
        return weeklySetmealMapper.selectWeeklySetmealList(weeklySetmeal);
    }

    @Override
    public List<WeekDayVo> selectMealTableListByDayNumbers(Long userId, List<Long> dayNumbers) {
        return buildWeekData(weeklySetmealMapper.selectMealTableListByDayNumbers(userId,dayNumbers));
    }
    public List<WeekDayVo> buildWeekData(List<MealTableVo> flatList) {
        Map<Integer, WeekDayVo> dayMap = new TreeMap<>();

        for (MealTableVo item : flatList) {
            int dayNumber = item.getDayNumber();
            WeekDayVo dayVo = dayMap.computeIfAbsent(dayNumber, k -> {
                WeekDayVo vo = new WeekDayVo();
                vo.setLabel(item.getWeekDay());
                vo.setDayNumber(dayNumber);
                vo.setMeals(new ArrayList<>());
                return vo;
            });

            // 找到已有的 type=中餐 或 type=晚餐 的组
            MealGroupVo mealGroup = dayVo.getMeals().stream()
                    .filter(m -> m.getType().equals(item.getType()))
                    .findFirst()
                    .orElseGet(() -> {
                        MealGroupVo newGroup = new MealGroupVo();
                        newGroup.setType(item.getType());
                        newGroup.setPackages(new ArrayList<>());
                        dayVo.getMeals().add(newGroup);
                        return newGroup;
                    });

            MealPackageVo pkg = new MealPackageVo();
            BeanUtils.copyProperties(item, pkg);
            mealGroup.getPackages().add(pkg);
        }

        return new ArrayList<>(dayMap.values());
    }


    /**
     * 新增每周已制定套餐记录
     *
     * @param weeklySetmeal 每周已制定套餐记录
     * @return 结果
     */
    @Override
    public int insertWeeklySetmeal(WeeklySetmeal weeklySetmeal)
    {
        weeklySetmeal.setCreateTime(DateUtils.getNowDate());
        return weeklySetmealMapper.insertWeeklySetmeal(weeklySetmeal);
    }

    /**
     * 修改每周已制定套餐记录
     *
     * @param weeklySetmeal 每周已制定套餐记录
     * @return 结果
     */
    @Override
    public int updateWeeklySetmeal(WeeklySetmeal weeklySetmeal)
    {
        weeklySetmeal.setUpdateTime(DateUtils.getNowDate());
        return weeklySetmealMapper.updateWeeklySetmeal(weeklySetmeal);
    }

    /**
     * 批量删除每周已制定套餐记录
     *
     * @param ids 需要删除的每周已制定套餐记录主键
     * @return 结果
     */
    @Override
    public int deleteWeeklySetmealByIds(Long[] ids)
    {
        return weeklySetmealMapper.deleteWeeklySetmealByIds(ids);
    }

    /**
     * 删除每周已制定套餐记录信息
     *
     * @param id 每周已制定套餐记录主键
     * @return 结果
     */
    @Override
    public int deleteWeeklySetmealById(Long id)
    {
        return weeklySetmealMapper.deleteWeeklySetmealById(id);
    }

    @Override
    public CarouseDto getCarousel(Integer dateInt, String mealType, String mealMark) {
        return weeklySetmealMapper.getCarousel(dateInt,mealType,mealMark);
    }

    @Override
    public List<MealInfoVo> getMealInfo() {
        return weeklySetmealMapper.getMealInfo();
    }
}
